﻿using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JT1078.Gateway
{
    /// <summary>
    /// 向终端发送查询媒体资源指令，数据上传完成时通知
    /// </summary>
    public class MediaQueryCompleteCacheManager
    {
        private IMemoryCache _cache = new MemoryCache(new MemoryCacheOptions());
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"> 查询音视频资源列表指令的流水号</param>
        /// <returns></returns>
        public MediaQueryCompleteCacheItem Get(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                return null;

            if (_cache.TryGetValue<MediaQueryCompleteCacheItem>(key, out var video))
            {
                return video;
            }
           
            return null;
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <param name="key"> 查询音视频资源列表指令的流水号</param>
        public void Clear(string key)
        {
            if (_cache.TryGetValue(key, out var _))
                _cache.Remove(key);
        }

        /// <summary>
        /// 设置缓存，绝对过期时间,保存10分钟
        /// </summary>
        /// <param name="key"> 查询音视频资源列表指令的流水号</param>
        /// <param name="value">缓存值</param>
        public void Set(string key, MediaQueryCompleteCacheItem value)
        {
            Clear(key); 
            _cache.Set(key, value, new TimeSpan(0, 10, 0));
        }
    }
    public class MediaQueryCompleteCacheItem
    {
        public string SimNo { get; set; }
        public VideoOrderAck Order { get; set; }
    }
}
